package cn.com.text;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import org.litepal.crud.DataSupport;

import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import cn.com.text.Dao.MyPost;
import cn.com.text.Dao.News;
import cn.com.text.Util.ImageHelper;
import cn.com.text.Util.MySQLConnections;

public class NewsContentActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private ImageView imageViewContent;

    public static void actionStart(Context context, String newsTitle, String newsContent, String newsImage) {
        Intent intent = new Intent(context, NewsContentActivity.class);
        intent.putExtra("news_title", newsTitle);
        intent.putExtra("news_content", newsContent);
        intent.putExtra("news_image", newsImage);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_content);
        imageViewContent = (ImageView) findViewById(R.id.imageContent);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        String newsTitle = getIntent().getStringExtra("news_title");
        String newContent = getIntent().getStringExtra("news_content");
        List<MyPost> myPosts = DataSupport.where("newstitle = ?", newsTitle).find(MyPost.class);
        String imageBuff = "";
        if (myPosts.size() != 0) {
            imageBuff = myPosts.get(0).getImagePost();
        }
        if (imageBuff.equals("")) {
            imageViewContent.setImageResource(R.drawable.img_2);
        } else {
            imageViewContent.setImageBitmap(ImageHelper.stringToBitmap(imageBuff));
        }

        NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager()
                .findFragmentById(R.id.news_content_fragment);
        newsContentFragment.refresh(newsTitle, newContent);
        toolbar.setTitle(newsTitle);
        toolbar.setSubtitle(newsTitle);
        setSupportActionBar(toolbar);
//        toolbar.setNavigationIcon(R.drawable.ic_action_n);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return false;
    }
}
